<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use App\User;

class AdminController extends Model
{
    // 添加页 
    public function add(){
    	return view('admin.add');
    }

    // 列表显示
    public function list(){
    	return view('admin.list');
    }

    // 图片显示
    public function img(){
    	return view('admin.img');
    }

    // 标签显示
    public function tab(){
    	return view('admin.tab');
    }

    //其他组件信息
    public function element(){
    	return view('admin.element');
    }

    // 修改密码
    public function pass(){
        if($input = Input::all()){
            $rules = [
                'password' => 'required|between:6,20|confirmed',
            ];
            $message = [
                'password.required' => '新密码不能为空！',
                'password.between'  => '新密码在6-20位！',
                'password.confirmed'=> '新密码与确认密码不一致！',
            ];
            $validator = Validator::make($input,$rules,$message);
            if($validator->passes()){
                $admin = User::first();
                $_password = decrypt($admin->user_pass);
                if($input['password_o'] === $_password){
                    $admin->user_pass = encrypt($input['password']);
                    $admin->save();
                    return redirect('admin/pass')->with('success','修改成功');
                } else {
                    return back()->with('errors','原密码错误！');
                }
            } else {
                //dd($validator->errors()->all());
                return back()->withErrors($validator);
            }
        }
    	return view('admin.pass');
    }
    // 退出登录
    public function checkout(){
        session()->flush();
        return redirect('login');
    }
}
